﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class Admin_Default2 : System.Web.UI.Page
{
    clsDBOperation objDBOp = new clsDBOperation();
    Hashtable hstParameters = new Hashtable();
    DataSet dsadmin = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dsGolbal"] == null)
        {
            Response.Redirect("../");
        }
        dsadmin = (DataSet)Session.Contents["dsGolbal"];
        if (!IsPostBack)
        {
            FillCategory();
        }

    }

    private void FillCategory()
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_Category");
            dssave = (DataSet)objDBOp.GetDataset("sp_Movie", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {

                ddlCategory.DataTextField = "CategoryName";
                ddlCategory.DataValueField = "CategoryID";
                ddlCategory.DataSource = dssave;
                ddlCategory.DataBind();
                ddlCategory.Items.Insert(0, new ListItem("---Select Category---", "0"));
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillSubCategory();
    }

    private void FillSubCategory()
    {

        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_Sub_Category");
            hstParameters.Add("FK_CategoryID", Convert.ToInt32(ddlCategory.SelectedValue));
            dssave = (DataSet)objDBOp.GetDataset("sp_Movie", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {

                ddlSubCategory.DataTextField = "SubCategoryName";
                ddlSubCategory.DataValueField = "SubCategoryID";
                ddlSubCategory.DataSource = dssave;
                ddlSubCategory.DataBind();
                ddlSubCategory.Items.Insert(0, new ListItem("---Select Sub Category---", "0"));
            }
            else
            {
                ddlSubCategory.Items.Clear();
                ddlSubCategory.Items.Insert(0, new ListItem("---Not Found---", "0"));

            }

        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }
    protected void ddlSubCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetAlbum();
    }

    private void GetAlbum()
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_Song_Album");
            hstParameters.Add("FK_SubCategoryID", Convert.ToInt32(ddlSubCategory.SelectedValue));
            dssave = (DataSet)objDBOp.GetDataset("sp_Movie", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {

                grdMovie.DataSource = dssave;
                grdMovie.DataBind();
                lblmsg.Text = string.Empty;
            }
            else
            {
                lblmsg.Text = "No Records Found For Sub Category " + ddlSubCategory.SelectedItem.Text;
                grdMovie.DataSource = null;
                grdMovie.DataBind();

            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }
    protected void btnAddMovie_Click(object sender, EventArgs e)
    {
        Save();
        //GetAlbum();
    }

    private void Save()
    {
        try
        {
            //string PATH = Request.ServerVariables["APPL_PHYSICAL_PATH"].Trim();
            //string fileName = Path.GetFileName(FileUploadAlbumCover.FileName);
            //FileUploadAlbumCover.SaveAs(PATH + "Admin/AlbumCover/" + txtAlbumName.Text + fileName);

            int IsSaved;
            hstParameters.Clear();
            hstParameters.Add("Action", "Insert_Movie");
            hstParameters.Add("UserName", dsadmin.Tables[0].Rows[0][1].ToString());
            hstParameters.Add("FK_SubCategoryID", Convert.ToInt32(ddlSubCategory.SelectedValue));
            hstParameters.Add("FK_ServerID", Convert.ToInt32(ddlServer.SelectedValue));
            hstParameters.Add("MovieName", txtMovieName.Text);
            hstParameters.Add("MovieYear",Convert.ToInt32(txtMovieYear.Text));
            hstParameters.Add("MovieSize", txtMovieSize.Text);
            hstParameters.Add("MovieDescription", FCKeditor2.Value);
            hstParameters.Add("MovieCoverPath", FileUploadMovieCover.Text);
            hstParameters.Add("MovieGenre", txtMovieGenre.Text);
            hstParameters.Add("MovieOwner", txtMovieOwner.Text);

            IsSaved = Convert.ToInt32(objDBOp.ExecuteNonQuery("sp_Movie ", CommandType.StoredProcedure, hstParameters));
            if (IsSaved > 0)
                lblmsg.Text = "Record Saved Successfully";
            txtMovieName.Text = string.Empty;
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }
}
